package com.zhiguang.cms.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhiguang.cms.entity.Article;
import com.zhiguang.cms.entity.Category;
import com.zhiguang.cms.entity.Channel;
import com.zhiguang.cms.entity.Comment;

public interface ArticleMapper {

	/**
	 * 
	 * @return
	 */
	List<Article> list();

	Article getById(int id);

	/**
	 * 更改状态  0 待审核  1 审核通过 2 审核拒绝
	 * @param id
	 * @param status
	 * @return
	 */
	@Update("UPDATE cms_article SET `status`=#{status} WHERE id=#{id} ")
	int updateStatus(@Param("id")int id, @Param("status")int status);

	/**
	 * 
	 * @param id
	 * @param status  0 非热门  1 热门
	 * @return
	 */
	@Update("UPDATE cms_article SET `hot`=#{status} WHERE id=#{id} ")
	int updateHot(@Param("id")int id, @Param("status")int status);

	
	@Select("SELECT id ,name FROM cms_channel ORDER BY id ASC ")
	List<Channel> listChannels();

	@Select("SELECT id ,name FROM cms_category "
			+ " WHERE channel_id=#{value} ORDER BY id ASC ")
	List<Category> listCats(int channelId);

	@Insert("INSERT INTO cms_article(title,content,picture,channel_id,category_id,user_id,hits,hot,status,deleted,created,updated)"
			+ " VALUES(#{title},#{content},#{picture},#{channelId},#{categoryId},#{userId},0,0,0,0,now(),now())")
	
	int add(Article article);
	
	

	/**
	 * 
	 * @param userId
	 * @return
	 */
	List<Article> listByUser(Integer userId);
	
	/**
	 * 删除文章  逻辑删除
	 * @param id
	 */
	@Update("UPDATE cms_article SET deleted=1 WHERE id=#{value} ")
	void delete(int id);
	
	List<Article> last(int n);
	
	/*热门文章*/
	List<Article> hotList();

	/**
	 *  
	 * @param channelId
	 * @param categoryId
	 * @return
	 */
	List<Article> listBy2C(@Param("channelId") int channelId, @Param("categoryId")  int categoryId);

	/**
	 * 添加评论
	 * @param comment
	 * @return
	 */
	@Insert("INSERT INTO cms_comment(articleId,userId,content,created) "
			+ "VALUES(#{articleId},#{userId},#{content},now() )")
	int addComment(Comment comment);
	
	/**
	 * 增加文章的评论数量
	 * @param id 文章的id
	 */
	@Update("UPDATE cms_article SET commentCnt=commentCnt+1 WHERE id=#{value} ")
	void increaseCommentCnt(int id);
	
	
	/**
	 * 根据文章id列出来所有的评论 
	 * @param articleId
	 * @return
	 */
	@Select("SELECT id,articleId,userId,content,created "
			+ " FROM cms_comment WHERE articleId=#{value} ORDER BY created DESC ")
	List<Comment> listCommentByArticleId(int articleId);
	
}
